package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.zsyh.domain.ZsyhFactoryPay;
import com.ruoyi.zsyh.service.IZsyhFactoryPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhFactoryPayrel;
import com.ruoyi.zsyh.service.IZsyhFactoryPayrelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 厂家服务费付款记录Controller
 * 
 * @author yuexl
 * @date 2021-12-24
 */
@Controller
@RequestMapping("/zsyh/factoryPayRel")
public class ZsyhFactoryPayrelController extends BaseController
{
    private String prefix = "zsyh/factoryPayRel";

    @Autowired
    private IZsyhFactoryPayrelService zsyhFactoryPayrelService;
    @Autowired
    private IZsyhFactoryPayService zsyhFactoryPayService;

    @RequiresPermissions("zsyh:factoryPayRel:view")
    @GetMapping()
    public String factoryPayRel()
    {
        return prefix + "/factoryPayRel";
    }

    /**
     * 查询厂家服务费付款记录列表
     */
    @RequiresPermissions("zsyh:factoryPayRel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhFactoryPayrel zsyhFactoryPayrel)
    {
        startPage();
        List<ZsyhFactoryPayrel> list = zsyhFactoryPayrelService.selectZsyhFactoryPayrelList(zsyhFactoryPayrel);
        return getDataTable(list);
    }

    /**
     * 导出厂家服务费付款记录列表
     */
    @RequiresPermissions("zsyh:factoryPayRel:export")
    @Log(title = "厂家服务费付款记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhFactoryPayrel zsyhFactoryPayrel)
    {
        List<ZsyhFactoryPayrel> list = zsyhFactoryPayrelService.selectZsyhFactoryPayrelList(zsyhFactoryPayrel);
        ExcelUtil<ZsyhFactoryPayrel> util = new ExcelUtil<ZsyhFactoryPayrel>(ZsyhFactoryPayrel.class);
        return util.exportExcel(list, "厂家服务费付款记录数据");
    }

    /**
     * 新增厂家服务费付款记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增厂家服务费付款记录
     */
    @GetMapping("/addPayRel")
    public String addPayRel(String id,ModelMap mmap)
    {
        ZsyhFactoryPay zsyhFactoryPay = zsyhFactoryPayService.selectZsyhFactoryPayById(id);
        mmap.put("zsyhFactoryPay",zsyhFactoryPay);
        return prefix + "/add";
    }

    /**
     * 新增保存厂家服务费付款记录
     */
    @Transactional
    @RequiresPermissions("zsyh:factoryPayRel:add")
    @Log(title = "厂家服务费付款记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhFactoryPayrel zsyhFactoryPayrel)
    {
        //设置操作人
        zsyhFactoryPayrel.setOperateId(ShiroUtils.getSysUser().getUserName());
        int num = zsyhFactoryPayrelService.insertZsyhFactoryPayrel(zsyhFactoryPayrel);
        if(num >0){
           //修改主表信息
            ZsyhFactoryPay zsyhFactoryPay = zsyhFactoryPayService.selectZsyhFactoryPayById(zsyhFactoryPayrel.getPayId());
            zsyhFactoryPay.setPayOff(zsyhFactoryPay.getPayOff().add(zsyhFactoryPayrel.getDiscountMoney()).add(zsyhFactoryPayrel.getPaidMoney()));
            zsyhFactoryPay.setPayEnd(zsyhFactoryPay.getPayEnd().subtract(zsyhFactoryPayrel.getDiscountMoney()).subtract(zsyhFactoryPayrel.getPaidMoney()));
            zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay);
        }
        return toAjax(num);
    }

    /**
     * 修改厂家服务费付款记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhFactoryPayrel zsyhFactoryPayrel = zsyhFactoryPayrelService.selectZsyhFactoryPayrelById(id);
        mmap.put("zsyhFactoryPayrel", zsyhFactoryPayrel);
        return prefix + "/edit";
    }

    /**
     * 修改保存厂家服务费付款记录
     */
    @Transactional
    @RequiresPermissions("zsyh:factoryPayRel:edit")
    @Log(title = "厂家服务费付款记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhFactoryPayrel zsyhFactoryPayrel)
    {
        //修改结算记录
        ZsyhFactoryPayrel oldPayRel = zsyhFactoryPayrelService.selectZsyhFactoryPayrelById(zsyhFactoryPayrel.getId());
        ZsyhFactoryPay zsyhFactoryPay = zsyhFactoryPayService.selectZsyhFactoryPayById(oldPayRel.getPayId());
        zsyhFactoryPay.setPayOff(zsyhFactoryPay.getPayOff().subtract(oldPayRel.getDiscountMoney()).subtract(oldPayRel.getPaidMoney()));
        zsyhFactoryPay.setPayEnd(zsyhFactoryPay.getPayEnd().add(oldPayRel.getDiscountMoney()).add(oldPayRel.getPaidMoney()));
        int num = zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay);
        if(num >0){
            zsyhFactoryPay.setPayOff(zsyhFactoryPay.getPayOff().add(zsyhFactoryPayrel.getDiscountMoney()).add(zsyhFactoryPayrel.getPaidMoney()));
            zsyhFactoryPay.setPayEnd(zsyhFactoryPay.getPayEnd().subtract(zsyhFactoryPayrel.getDiscountMoney()).subtract(zsyhFactoryPayrel.getPaidMoney()));
            zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay);
        }
        return toAjax(zsyhFactoryPayrelService.updateZsyhFactoryPayrel(zsyhFactoryPayrel));
    }

    /**
     * 删除厂家服务费付款记录
     */
    @RequiresPermissions("zsyh:factoryPayRel:remove")
    @Log(title = "厂家服务费付款记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //修改结算记录
        ZsyhFactoryPayrel oldPayRel = zsyhFactoryPayrelService.selectZsyhFactoryPayrelById(ids);
        ZsyhFactoryPay zsyhFactoryPay = zsyhFactoryPayService.selectZsyhFactoryPayById(oldPayRel.getPayId());
        zsyhFactoryPay.setPayOff(zsyhFactoryPay.getPayOff().subtract(oldPayRel.getDiscountMoney()).subtract(oldPayRel.getPaidMoney()));
        zsyhFactoryPay.setPayEnd(zsyhFactoryPay.getPayEnd().add(oldPayRel.getDiscountMoney()).add(oldPayRel.getPaidMoney()));
        int num = zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay);
        if(num >0){
            zsyhFactoryPayrelService.deleteZsyhFactoryPayrelByIds(ids);
        }
        return toAjax(num);
    }
}
